package com.yuanfeng.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.payment.entity.PayDistributorEarningsEntity;
import com.yuanfeng.payment.mapper.PayDistributorEarningsMapper;
import com.yuanfeng.payment.mapper.PayDistributorStatementMapper;
import com.yuanfeng.payment.service.PayDistributorEarningsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 *
 */
@Service
public class PayDistributorEarningsServiceImpl extends ServiceImpl<PayDistributorEarningsMapper, PayDistributorEarningsEntity>
    implements PayDistributorEarningsService{

    @Autowired
    private PayDistributorStatementMapper payDistributorStatementMapper;
    @Override
    public ResponseResult queryRecord(Map<String, Object> map) {
        IPage<PayDistributorEarningsEntity> page=this.page(new Query<PayDistributorEarningsEntity>().getPage(map),
                                    new LambdaQueryWrapper<PayDistributorEarningsEntity>().eq(PayDistributorEarningsEntity::getStatementNumber, map.get("statementNumber"))
                                    .orderByDesc(PayDistributorEarningsEntity::getPaymentTime));
        return ResponseResult.success(PageUtils.getPage(page));
    }

    @Override
    public ResponseResult verifyStatement(Map<String, Object> param) {
        UserInfoVO userInfoVO= ParseTokenUtils.getPlatformUser(param.get("token").toString());
        if(null==userInfoVO){
            return ResponseResult.fail(BizCodeEnume.USER_DOES_NOT_EXIST);
        }

        param.put("verifyBy",(StringUtils.isEmpty(userInfoVO.getUserRealname()) ? "未知" : userInfoVO.getUserRealname()));
//        param.setVerifyBy("ceshi");
        this.baseMapper.update(null,new LambdaUpdateWrapper<PayDistributorEarningsEntity>()
                        .set(PayDistributorEarningsEntity::getState,param.get("state").toString())
                        .eq(PayDistributorEarningsEntity::getStatementNumber, param.get("statementNumber").toString()));
        payDistributorStatementMapper.updateStatementState(param);
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);

    }
}




